package com.gzk.oeno.ihm.vin.form;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.gzk.oeno.business.codif.CodifVin;
import com.gzk.oeno.business.codif.CodifVinManager;
import com.gzk.oeno.ihm.common.HibernateEditForm;
import com.gzk.oeno.tech.ui.Formator;
import com.gzk.utils.CustomException;
import com.gzk.utils.misc.StringUtils;

public class CodifVinForm extends HibernateEditForm<Integer, CodifVin>{

	private JTextField nameField;
	private JTextArea linkArea;
	private JTextArea descriptionArea;
	
	public CodifVinForm() {
		super(CodifVinManager.getInstance());
	}

	@Override
	protected JPanel buildForm() {
		JPanel pane = new JPanel(new GridBagLayout());
		
		GridBagConstraints gc = getNewConstraint();
		addLabelAndField(pane, Formator.boldLabel("Nom* :"), getNameField(), gc);
		addLabelAndField(pane, new JLabel("Lien dynamique :"), getLinkArea(), gc);
		addLabel(pane, new JLabel("Renseigner "+CodifVinManager.ESCAPE_CODE+" dans le lien dynamique"), gc);
		addLabelAndField(pane, new JLabel("Description :"), getDescriptionArea(), gc);
		
		return pane;
	}

	@Override
	protected CodifVin fromForm() throws CustomException {
		CodifVin codif = original != null ? original : new CodifVin();
		
		codif.setName(getNameField().getText());
		codif.setDescription(getDescriptionArea().getText());
		codif.setLienDynamique(getLinkArea().getText());
		
		return codif;
	}

	@Override
	protected void toForm(CodifVin object) {
		getNameField().setText(object.getName());
		getDescriptionArea().setText(object.getDescription());
		getLinkArea().setText(object.getLienDynamique());
	}

	@Override
	protected void cleanForm() {
		getNameField().setText(null);
		getDescriptionArea().setText(null);
		getLinkArea().setText(null);
	}

	@Override
	protected boolean isEditable() {
		return StringUtils.isNotEmpty(getNameField().getText());
	}

	
	protected JTextField getNameField() {
		if (nameField == null){
			nameField = new JTextField(25);
			nameField.addKeyListener(autoRefreshKListener);
		}
		return nameField;
	}

	protected JTextArea getLinkArea() {
		if (linkArea == null){
			linkArea =  new JTextArea(3, 25);
		}
		return linkArea;
	}

	protected JTextArea getDescriptionArea() {
		if (descriptionArea == null){
			descriptionArea = new JTextArea(8, 25);
		}
		return descriptionArea;
	}
	
}
